package com.siecom.module;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Environment;

import com.siecom.bean.IdentityInfoBean;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * 生成身份证图片
 * Created by zhq on 2016/8/23.
 */
public class IDPicture {

    private static String SDPath = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/";

    public static Bitmap readFromSD(String filename) {
        File file = new File((SDPath + filename));
        if (file.exists()) {
            Bitmap bm = BitmapFactory.decodeFile(SDPath + filename).copy(
                    Bitmap.Config.ARGB_8888, true);

            return bm;
        }
        return null;

    }

    public static Bitmap createBackPic(Bitmap back,
                                       IdentityInfoBean bean) {

        Bitmap t= back.copy(Bitmap.Config.ARGB_8888, true);
        Canvas canvasTemp = new Canvas(t);
        Paint p = new Paint();
        p.setColor(Color.BLACK);
        p.setAntiAlias(true);// 去除锯齿
        p.setFilterBitmap(true);// 对位图进行滤波处理
        p.setTextSize(32);
        float tX = 425;
        float tY = 495;
        canvasTemp.drawText(bean.getIdOrg(), tX, tY, p);

        tX = 425;
        tY = 575;
        canvasTemp
                .drawText(bean.getBeginDate() + "--" + bean.getEndDate(), tX, tY, p);
        canvasTemp.save();
        return t;

    }

    public static Bitmap createFrontPic(Bitmap front,
                                        IdentityInfoBean bean) {

        Bitmap t= front.copy(Bitmap.Config.ARGB_8888, true);
        Canvas canvasTemp = new Canvas(t);
        Paint p = new Paint();
        p.setColor(Color.BLACK);
        p.setAntiAlias(true);// 去除锯齿
        p.setFilterBitmap(true);// 对位图进行滤波处理
        p.setTextSize(32);
        float tX = 200;
        float tY = 128;
        canvasTemp.drawText(bean.fullName, tX, tY, p);

        tX = 200;
        tY = 200;
        canvasTemp.drawText(bean.getSex(), tX, tY, p);

        tX = 425;
        tY = 200;
        canvasTemp.drawText(bean.nation, tX, tY, p);

        tX = 200;
        tY = 280;
        canvasTemp.drawText(bean.birthday.substring(0, 4), tX, tY, p);

        tX = 350;
        tY = 280;
        canvasTemp.drawText(bean.birthday.substring(4, 6), tX, tY, p);

        tX = 440;
        tY = 280;
        canvasTemp.drawText(bean.birthday.substring(6, 8), tX, tY, p);

        tX = 200;
        tY = 360;
        if (bean.getIdAddr().length() > 15) {

            canvasTemp.drawText(bean.getIdAddr().substring(0, 10), tX, tY, p);
            tY += 40;
            canvasTemp.drawText(bean.getIdAddr().substring(10), tX, tY, p);

        } else
            canvasTemp.drawText(bean.getIdAddr(), tX, tY, p);

        tX = 670;
        tY = 120;
        canvasTemp.drawBitmap(big(bitmapTransport(bean.getIcon())), tX, tY, p);
        tX = 384;
        tY = 564;
        canvasTemp.drawText(bean.idNo, tX, tY, p);
        canvasTemp.save();
        return t;
    }

    private static Bitmap big(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.postScale(2.5f, 2.5f); // 长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), matrix, true);
        return resizeBmp;
    }
    public static Bitmap resizeImage(Bitmap bitmap, int w, int h) {
        Bitmap BitmapOrg = bitmap;

        int width = BitmapOrg.getWidth();
        int height = BitmapOrg.getHeight();

        if(width<w){
            return bitmap;
        }
        int newWidth = w;
        int newHeight = h;
        if ( width >= height)
        {
            newWidth = w;
            newHeight = (int) Math.round((height * w * 1.0 / width));
        }
        else
        {
            newHeight = h;
            newWidth = (int) Math.round((width * h * 1.0 / height));
        }
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
                height, matrix, true);
        return resizedBitmap;
    }


    public static void saveBitmap(Bitmap mBitmap, String bitName) {

        String path = "/outpic/";

        File file = new File(SDPath + path);
        file.mkdir();

        File f = new File(SDPath + path + bitName + ".jpg");
        if (f.exists()) {
            f.delete();
        }
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static int COLOR_RANGE = 215; // 图片中接近白色的程度

    // 判断当前点是否为白色
    private static boolean colorInRange(int color) {
        int red = (color & 0xff0000) >> 16;
        int green = (color & 0x00ff00) >> 8;
        int blue = (color & 0x0000ff);
        if (red >= COLOR_RANGE && green >= COLOR_RANGE && blue >= COLOR_RANGE)
            return true;
        return false;
    }

    private static Bitmap bitmapTransport(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] argb = new int[width * height];
        bitmap.getPixels(argb, 0, width, 0, 0, width, height);
        for (int y = 0; y < height; y++)
            for (int x = 0; x < width; x++) {
                int index = y * width + x;
                int r = Color.red(argb[index]);
                int g = Color.green(argb[index]);
                int b = Color.blue(argb[index]);
                if (colorInRange(argb[index])) {
                    argb[index] = 0x00000000 | (r << 16) | (g << 8) | b;
                } else {
                    argb[index] = 0xFF000000 | (r << 16) | (g << 8) | b;
                }
            }
        return Bitmap
                .createBitmap(argb, width, height, Bitmap.Config.ARGB_8888);
    }

}
